package com.GUIWork;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.dbConnection.theConnection;

import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class searchFrame extends JFrame {
	
	theConnection conn;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private searchFrame sframe = this;
	
	public searchFrame(final theConnection conn, final JFrame frame) {
		setTitle("Search For Items");
		this.conn = conn;
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblKeyword = new JLabel("Keyword");
		lblKeyword.setBounds(10, 11, 55, 14);
		panel.add(lblKeyword);
		
		textField = new JTextField();
		textField.setBounds(123, 8, 198, 20);
		panel.add(textField);
		textField.setColumns(30);
		
		JLabel lblCategory = new JLabel("Category");
		lblCategory.setBounds(10, 56, 46, 14);
		panel.add(lblCategory);
		
		JComboBox category = new JComboBox();
		category.setBounds(123, 53, 86, 20);
		category.addItem("");
		panel.add(category);
		
		JLabel lblMinPrice = new JLabel("Minimum Price $");
		lblMinPrice.setBounds(10, 93, 75, 14);
		panel.add(lblMinPrice);
		
		textField_1 = new JTextField();
		textField_1.setBounds(123, 90, 86, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblMaxPrice = new JLabel("Maximum Price $");
		lblMaxPrice.setBounds(10, 124, 79, 14);
		panel.add(lblMaxPrice);
		
		textField_2 = new JTextField();
		textField_2.setBounds(123, 121, 86, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblConditionAtLeast = new JLabel("Condition At Least");
		lblConditionAtLeast.setBounds(10, 169, 90, 14);
		panel.add(lblConditionAtLeast);
		
		JComboBox condition = new JComboBox();
		condition.setBounds(123, 166, 86, 20);
		condition.addItem("");
		condition.addItem("New");
		condition.addItem("Very Good");
		condition.addItem("Good");
		condition.addItem("Fair");
		condition.addItem("Poor");
		panel.add(condition);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(253, 230, 179, 25);
		panel.add(splitPane);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String query = doSearch();
				conn.askDB(query);
				searchResults results = new searchResults(conn);
				results.setVisible(true);
				results.setBounds(100, 100, 450, 300);
				results.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				sframe.dispose();
			}
		});
		splitPane.setLeftComponent(btnSearch);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(frame!=null){
					frame.setVisible(true);
					sframe.setVisible(false);
					sframe.dispose();
				}
				else{
					RegUserFrame uframe = new RegUserFrame(conn);
					uframe.setVisible(true);
					uframe.setBounds(100, 100, 450, 300);
					uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					sframe.dispose();
				}
			}
		});
		splitPane.setRightComponent(btnCancel);
	}
	
	public String doSearch(){
		//TODO Write code to get all data (if applicable) and build a query to ask the DB
		return "";
	}
}
